﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace homeIMSClient
{
    public partial class main : Form
    {
        private Model.user _activeUser;

        //constructor takes in Object user to hydrate form
        public main()
        {
            //before initialization, run modal login form to gain validation and information for the user
            //instantiate private var w/ userinfo from service.
            
            logReg login = new logReg();
            login.ShowDialog();

            //initialize components if user variable is set to a user
            if (login.emptyUser != null)
            {
                InitializeComponent();
                hydrateForm(login.emptyUser);
            }
            else
            {
                //if, for some god awful reason, the login form is closed w/o a user set, exit application
                Application.Exit();
            }     
        }

        //method is used for hydrating form w/ user and asset information
        private void hydrateForm(homeIMSRef.userInfo newUser)
        {
            //change text of form and label
            this.Text = "HomeIMS - " + newUser.userName.ToString();
            
            //send userInfo as parameter for new creation of user class and add parent node of userName
            _activeUser = new Model.user(newUser);

            TreeNode root = new TreeNode(_activeUser.UserName.ToString());

            tviewAssets.Nodes.Add(root);
            //take Type from each asset and add it as child node
            foreach (Model.asset asset in _activeUser.assetList)
            {
                
                //check previous nodes to see if added already
                //variable will be set to true if node was added already
                bool isAdded = false;
                for (int i = 0; i < root.Nodes.Count; i++ )
                {
                    if (root.Nodes[i].Tag.ToString() == asset.TypeID.ToString())
                    {
                        //set bool to true and break statement
                        isAdded = true;
                        break;
                    }
                }
                //check is added to see if node was already added
                switch (isAdded)
                {
                    case true:
                        {
                            //if added, send through addAssetDetail method
                            addName(asset);
                        }
                        break;
                    case false:
                        {
                            //if not added, add Type and send through addAssetDetail method
                            TreeNode typeNode = new TreeNode(asset.Type.ToString());
                            typeNode.Tag = asset.TypeID.ToString();
                            root.Nodes.Add(typeNode);

                            addName(asset);
                        }
                        break;
                }   
            }
            //expand list view to children
            root.Expand();
        }

        //method will add asset node in treeView
        private void addName(Model.asset asset)
        {
            //start adding necessary detail nodes and information for details
            TreeNodeCollection root = tviewAssets.Nodes[0].Nodes;

            TreeNode assetNode = new TreeNode(asset.Name.ToString());
            assetNode.Tag = asset.AssetID.ToString();

            foreach (TreeNode node in root)
            {
                if (node.Text == asset.Type)
                {
                    node.Nodes.Add(assetNode);
                    break;
                }
            }
        }

        //method will check node to see if asset node
        private bool CheckNode (TreeNode node)
        {

            if (node.Nodes.Count == null || node.Nodes.Count == 0)
            {
                //INSERT FORM CREATION HERE
                
                return true;
            }
            else
            {
                return false;
            }    
        }
        
        //method will take node information and pass it through to form creation
        private void CreateForm(TreeNode node)
        {
            Model.asset selectedAsset = null;
            //take node information and find asset in _activeUser
            foreach (Model.asset asset in _activeUser.assetList)
            {
                if (asset.AssetID.ToString() == node.Tag.ToString())
                {
                    //if found, fill variable and break
                    selectedAsset = asset;
                    break;
                    
                }
            }

            //check for asset being filled
            if (selectedAsset == null)
            {
                //if null, send error
                MessageBox.Show("ERROR: Asset not found");
            }
            else
            {

                //create new form
                frmAsset assetFormTest = new frmAsset(_activeUser, selectedAsset);

                //set parent
                assetFormTest.MdiParent = this;

                //show form
                assetFormTest.Show();
            }
        }

        //method will create new form for newAsset
        private void CreateForm()
        {
            //create new assetForm
            frmAsset assetFormNew = new frmAsset(_activeUser);

            //set mdi parent
            assetFormNew.MdiParent = this;

            //open form
            assetFormNew.Show();
        }

        //method will take node and fill information for asset
        private void FillAssetInfoPanel(TreeNode node)
        {
            //find asset in list
            foreach (Model.asset asset in _activeUser.assetList)
            {
                //check for assetID
                if (asset.AssetID.ToString() == node.Tag.ToString())
                {
                    //if tag is equal, take information and add it to label, then break loop
                    lblCreated.Text = "Created: " + asset.CreateDate.ToShortDateString();
                    lblLastUpdated.Text = "Last Updated: " + asset.lastUpdate.ToShortDateString();
                    lblPurchased.Text = "Purchase: " + asset.PurchDate.ToShortDateString();
                    lblValue.Text = "Value: $" + asset.RetailValue.ToString();
                    txtComments.Text = asset.Comments.ToString();

                    //enable edit button
                    btnEdit.Enabled = true;

                    //break loop
                    break;
                }
            }
        }

        //button will take selected asset node and send through method to open form
        private void btnEdit_Click(object sender, EventArgs e)
        {
            //push through create form
            CreateForm(tviewAssets.SelectedNode);
        }

        //action will select node where mouse location is and go through checking node
        private void tviewAssets_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) 
        {
            //select node
            tviewAssets.SelectedNode = e.Node;
            //check for asset node
            bool check = CheckNode(tviewAssets.SelectedNode);

            //switch result
            switch (check)
            {
                case true:
                    {
                        //if true, push through fill information
                        FillAssetInfoPanel(tviewAssets.SelectedNode);

        
                    }
                    break;
                case false:
                    {
                        //if false, disable edit button and clear lbls
                        btnEdit.Enabled = false;
                        lblCreated.Text = "Created: ";
                        lblLastUpdated.Text = "Last Updated: ";
                        lblPurchased.Text = "Purchased: ";
                        lblValue.Text = "Value: ";
                        txtComments.Text = "";
                        
                    }
                    break;
            }
        }

        private void tviewAssets_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //select node clicked
            tviewAssets.SelectedNode = e.Node;

            //send through create form
            CreateForm(tviewAssets.SelectedNode);
        }

        private void btnNewAsset_Click(object sender, EventArgs e)
        {
            //when new is clicked, send through createForm
            CreateForm();
        }

       
    }
}
